package com.zzs.server.controller;

import com.zzs.common.core.response.Result;
import com.zzs.server.pojo.bo.test.SaveTestCaseBO;
import com.zzs.server.pojo.bo.test.UpdateTestCaseBO;
import com.zzs.server.pojo.dto.test.SaveTestCaseDTO;
import com.zzs.server.pojo.dto.test.UpdateTestCaseDTO;
import com.zzs.server.pojo.vo.TestCaseVO;
import com.zzs.server.service.TestCaseService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author zzs
 * @date 2025/03/18
 */
@RestController
@RequestMapping("/server/case")
public class TestCaseController {
    @Autowired
    private TestCaseService testCaseService;

    @PostMapping
    public Result<Void> saveTestCase(@Valid @RequestBody SaveTestCaseDTO saveTestCaseDTO) {
        LocalDateTime now = LocalDateTime.now();
        SaveTestCaseBO saveTestCaseBO = SaveTestCaseBO.builder()
                .problemId(saveTestCaseDTO.getProblemId())
                .testCase(saveTestCaseDTO.getTestCase())
                .createTime(now)
                .updateTime(now)
                .build();
        testCaseService.saveTestCase(saveTestCaseBO);
        return Result.success();
    }

    @PutMapping
    public Result<Void> updateTestCase(@Valid @RequestBody UpdateTestCaseDTO updateTestCaseDTO) {
        LocalDateTime now = LocalDateTime.now();
        UpdateTestCaseBO updateTestCaseBO = UpdateTestCaseBO.builder()
                .problemId(updateTestCaseDTO.getProblemId())
                .testCase(updateTestCaseDTO.getTestCase())
                .updateTime(now)
                .build();
        testCaseService.updateTestCase(updateTestCaseBO);
        return Result.success();
    }

    @GetMapping("/{problemId}")
    public Result<TestCaseVO> getTestCase(@PathVariable("problemId") Long problemId) {
        TestCaseVO testCaseVO = testCaseService.getTestCase(problemId);
        return Result.success(testCaseVO);
    }

    @DeleteMapping("/{problemId}")
    public Result<Void> deleteTestCase(@PathVariable("problemId") Long problemId) {
        testCaseService.removeTestCase(problemId);
        return Result.success();
    }
}
